(function () {
    var serverData = {};
    var serverTable;
    var id;
    
	serverTable = jQuery('#dataTable').DataTable({
	    "serverSide": true,
	    "ordering": false,
	    "ajax": {
	        "dataType": "json",
	        "type": "get",
	        "cache": false,
	        "url":window.rootPath+ '/ui/FlowTotalInfo/getpage',
	        "data": function (d) {
	            return $.extend({}, d, serverData);
	        }
	    },
	    columns: [
	        {
	        	data: null
	        },
	        {
	            data: 'cContractNumber'
	        },
	        {
	        	data: 'cProductPlanNumber'
	        },
	        
	        {
	            data: 'cOperateDes'
	        },
	        {
	            data: 'nOrder'
	        },
	        {
	            data: 'dOperateTime',
	            render: function (data, type, full, meta) {
	                if (data)
	                    return moment(data).format('YYYY/MM/DD');
	                else
	                    return '';
	            }
	        },
	        {
	            data: 'nEmpID.cEmpName'
	        },
	        {
	            data: 'nDeptID.cDeptName'
	        },
	        {
	            data: 'cFlowConfigName'
	        },
	        {
	            data: 'cFlowConfigDes'
	        },
	        {
	            data: 'cBusinessTypeName'
	        },
	        {
	            data: 'cFlowName'
	        },
	        {
	            data: 'cFlowDes'
	        },
	        {
	            data: 'cOrderUnitName'
	        },
	        {
	        	data: 'cProductType'
	        }
	        ],
	    "drawCallback": function (oSettings) {
	        var k = 0;
	        var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
	
	        for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
	            $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
	            k++;
	        }
	    }
	});
    	
    	
  //////初始化设备的树结构//////////
    	var modalSetting = {
    		edit : {
    			enable : true,
    			showRemoveBtn : false,
    			showRenameBtn : false
    		},
    		data : {
    			simpleData : {
    				enable : true,
    				idKey : "id",
    				pIdKey : "parentId",
    				rootParentId : 0
    			},
    			key : {
    				name : "name",
    				memo : "memo"
    			}
    		},
    		check : {
    			enable : true,
    			chkStyle : "radio"

    		},
    		view : {
    			showLine : true
    		},
    		callback:{
    			onCheck: showNode
    		}
    	};
    	
    	var urltree =window.rootPath+"/ui/FlowTotalInfo/getTree";
    	var firstNode = [];
    	jQuery.ajax({
    		url: encodeURI(urltree),
    		cache: false,
    		type: "get",
    		dataType: "json",
    		contentType: "application/json;charset=utf-8",//解决返回数据中的中文乱码问题
    		success: function (data) {
    			if (data.deviceType.length > 0) {
    				for (var i = 0; i < data.deviceType.length; i++) {
    					  firstNode.push(data.deviceType[i]);
    				}
    			}
    			$.fn.zTree.init($("#deviceTree"), modalSetting, firstNode);
    		}
    	});
    	
    ////////点击显示节点相关的方案///////
    	function showNode(event, treeId, treeNode){
    		id=treeNode.id;
    		//name = treeNode.name;
    		//jQuery('#name').val(name);
    		//memo = treeNode.memo;
    		serverData.id=id;
    		//clearQueryServerData("clearui");
    		getServerDate();
    		serverTable.draw(true);
    	}
    	
    	var getServerDate=function(){
        	if(jQuery("#cContractNumber-search").val()) serverData.cContractNumber=jQuery("#cContractNumber-search").val();
        	if(jQuery("#cProductPlanNumber-search").val()) serverData.cProductPlanNumber=jQuery("#cProductPlanNumber-search").val();
        	if(jQuery("#cOperateDes-search").val()) serverData.cOperateDes=jQuery("#cOperateDes-search").val();
        	
        	if(jQuery("#nOrder-search").val()) serverData.nOrderStartNum=jQuery("#nOrder-search").val();
        	if(jQuery("#nOrder-end-search").val()) serverData.nOrderEndNum=jQuery("#nOrder-end-search").val();
        	
        	if(jQuery("#dOperateTime-start-search").val()) serverData.dOperateTimeStartDate=jQuery("#dOperateTime-start-search").val();
        	if(jQuery("#dOperateTime-end-search").val()) serverData.dOperateTimeEndDate=jQuery("#dOperateTime-end-search").val();
        	
        	if(jQuery("#cEmpName-search").val()) serverData.cEmpName=jQuery("#cEmpName-search").val();
        	if(jQuery("#cDeptName-search").val()) serverData.cDeptName=jQuery("#cDeptName-search").val();
        	if(jQuery("#cFlowConfigName-search").val()) serverData.cFlowConfigName=jQuery("#cFlowConfigName-search").val();
        	if(jQuery("#cFlowConfigDes-search").val()) serverData.cFlowConfigDes=jQuery("#cFlowConfigDes-search").val();
        	if(jQuery("#cBusinessTypeName-search").val()) serverData.cBusinessTypeName=jQuery("#cBusinessTypeName-search").val();
        	if(jQuery("#cFlowName-search").val()) serverData.cFlowName=jQuery("#cFlowName-search").val();
        	if(jQuery("#cFlowDes-search").val()) serverData.cFlowDes=jQuery("#cFlowDes-search").val();
        	if(jQuery("#cOrderUnitName-search").val()) serverData.cOrderUnitName=jQuery("#cOrderUnitName-search").val();
        	if(jQuery("#cProductType-search").val()) serverData.cProductType=jQuery("#cProductType-search").val();
    	}
    	
    	var clearQueryServerData=function(option){
    		if(option=='clearui'){
       		 	jQuery('input').val('');
                jQuery('select').val('');
    		}
        	if(serverData.cContractNumber) serverData.cContractNumber=null;
        	if(serverData.cProductPlanNumber) serverData.cProductPlanNumber=null;
        	if(serverData.cOperateDes) serverData.cOperateDes=null;
        	
        	if(serverData.nOrderStartNum) serverData.nOrderStartNum=null;
        	if(serverData.nOrderEndNum) serverData.nOrderEndNum=null;
        	
        	if(serverData.dOperateTimeStartDate) serverData.dOperateTimeStartDate=null;
        	if(serverData.dOperateTimeEndDate) serverData.dOperateTimeEndDate=null;
        	
        	if(serverData.cEmpName) serverData.cEmpName=null;
        	if(serverData.cDeptName) serverData.cDeptName=null;
        	if(serverData.cFlowConfigName) serverData.cFlowConfigName=null;
        	if(serverData.cFlowConfigDes) serverData.cFlowConfigDes=null;
        	if(serverData.cBusinessTypeName) serverData.cBusinessTypeName=null;
        	if(serverData.cFlowName) serverData.cFlowName=null;
        	if(serverData.cFlowDes) serverData.cFlowDes=null;
        	if(serverData.cOrderUnitName) serverData.cOrderUnitName=null;
        	if(serverData.cProductType) serverData.cProductType=null;
    	}
    	
    	// 查询
        jQuery('.searchBtn').on('click',function(){
        	getServerDate();
        	serverTable.draw();
        	getSum(urlsum);
        });
        
     // 撤销
        jQuery('#reset-btn').on('click',function(){
        	clearQueryServerData("clearui");
        	serverTable.draw();
        });
	
})();

//初始化查询条件
function initQueryParam(){
	if($('#endDate').val()== "") 
	$('#endDate').val(new Date().getFullYear()+"-"+((new Date().getMonth() + 1+"").length == 1 ? ("0"+(new Date().getMonth() + 1)) : new Date().getMonth() + 1) +"-"+new Date().getDate());
	if($('#startDate').val() == "")
	$('#startDate').val(new Date().getFullYear()+"-01-01");
}

function getSum(urlsum){
	$.ajax({
        type: "GET", 
        dataType: 'json',
        async: false, 
        url: encodeURI(urlsum),
        success: function(data) {
        	jQuery('#sum').val(data.sum);
        }
	});
}